package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.error.TeacherError;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-05
 */
@Api(description = "讲师服务")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有讲师信息")
    //1、查询讲师的所有数据
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "逻辑删除讲师信息")
    @DeleteMapping("/delete/{id}")
    public R removeTeacherById(@PathVariable("id") String id) {

        try {
            eduTeacherService.removeById(id);
            return R.ok();
        } catch (Exception e) {
            throw new GuliException(20001, "逻辑删除讲师信息异常处理");
        }
    }

    //3、分页查询
    @ApiOperation(value = "分页查询讲师信息")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current, @PathVariable Long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        IPage<EduTeacher> page = eduTeacherService.page(pageTeacher, null);
        List<EduTeacher> records = pageTeacher.getRecords();
        long total = pageTeacher.getTotal();
        Map hashMap = new HashMap();
        hashMap.put("total", total);
        hashMap.put("rows", records);
        return R.ok().data(hashMap);
    }

    //4、条件查询带分页
    @ApiOperation(value = "分页查询讲师信息")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(EduTeacher::getGmtCreate);

        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            queryWrapper.like("name", teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) {
            queryWrapper.eq("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            queryWrapper.le("gmt_create", teacherQuery.getEnd());
        }

        IPage<EduTeacher> page = eduTeacherService.page(pageTeacher, queryWrapper);
        List<EduTeacher> records = page.getRecords();
        long total = pageTeacher.getTotal();
        Map hashMap = new HashMap();
        hashMap.put("total", total);
        hashMap.put("rows", records);
        return R.ok().data(hashMap);
    }

    //5、添加讲师
    @ApiOperation(value = "新增讲师")
    @PutMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        try {
            eduTeacherService.save(eduTeacher);
            return R.ok();
        } catch (Exception e) {
            throw new GuliException(TeacherError.E_10001.getCode(),TeacherError.E_10001.getMsg());
        }
    }

    //6、获取讲师信息
    @ApiOperation(value = "获取讲师信息通过id值")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item", eduTeacher);
    }

    //7、修改讲师信息
    @ApiOperation(value = "修改讲师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

